﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using TMDT_Nhom1A.BUS;

namespace TMDT_Nhom1A.Customer
{
    public partial class ThanhToan_Buoc3_ChonVanChuyen : System.Web.UI.Page
    {
        private static TMDT_Nhom1A.TransportWS.TransportCompany[] transportCompanies;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //kiem tra co qua buoc 2 chưa
                Session["Step"] = 2;

                if (Session["Step"].ToString() == "2")
                {
                    //load WS tu van chuyen
                    TMDT_Nhom1A.TransportWS.TransportCompany[] itransportCompanies = CouponWebservice_BUS.GetTranports();
                    transportCompanies = itransportCompanies;
                    //do du lieu lên combobox
                    int count = 0; 
                    foreach (TMDT_Nhom1A.TransportWS.TransportCompany item in transportCompanies)
                    {
                        DrlTransportName.Items.Add(new ListItem(item.TenCongTyVanChuyen,item.MaCongTyVanChuyen));
                        foreach (TMDT_Nhom1A.TransportWS.TransportType transType in item.HinhThucVanChuyen)
	                    {
		                    DrlTransportType.Items.Add(new ListItem(transType.TenHinhThucVanChuyen,transType.MaHinhThucVanChuyen));
                            if (count == 0)
                            {
                                LbGia.Text = transType.Gia.ToString();
                                lbDonViTien.Text = transType.DonViTien;
                                count = 1;
                            }
	                    }               
                    }

                    
                    Success.Visible = true;
                    failure.Visible = false;
                    ThongBao.Visible = false;
                    btNext.Visible = true;
                    Session["Step"] = 3;
                }
                else
                {
                    ThongBao.Visible = true;
                    failure.Visible = false;
                    Success.Visible = false;
                }
            }
        }

        protected void btNext_Click(object sender, EventArgs e)
        {
            //luu van chuyen vao session 
            TMDT_Nhom1A.TransportWS.TransportCompany transCompany = new TMDT_Nhom1A.TransportWS.TransportCompany();
            transCompany.MaCongTyVanChuyen = DrlTransportName.SelectedItem.Value;
            transCompany.TenCongTyVanChuyen = DrlTransportName.SelectedItem.Text;
            transCompany.HinhThucVanChuyen = new TMDT_Nhom1A.TransportWS.TransportType[1];
            transCompany.HinhThucVanChuyen[0] = new TMDT_Nhom1A.TransportWS.TransportType();
            transCompany.HinhThucVanChuyen[0].MaHinhThucVanChuyen = DrlTransportType.SelectedItem.Value;
            transCompany.HinhThucVanChuyen[0].TenHinhThucVanChuyen = DrlTransportType.SelectedItem.Text;
            transCompany.HinhThucVanChuyen[0].Gia = float.Parse(LbGia.Text);
            transCompany.HinhThucVanChuyen[0].DonViTien = lbDonViTien.Text;

            Session["TransportCompany"] = transCompany;

            Response.Redirect("~/Customer/ThanhToan_buoc4_NganHang.aspx");
        }

        protected void DrlTransportType_SelectedIndexChanged(object sender, EventArgs e)
        {
            //load gia tuong ung voi gia van chuyen
            int transportId = int.Parse(DrlTransportName.SelectedItem.Value);
            int transportType = int.Parse(DrlTransportType.SelectedItem.Value);
            for (int i = 0; i < transportCompanies.Length; i++)
            {
                if (transportCompanies[i].MaCongTyVanChuyen == transportId.ToString())
                {
                    for (int j = 0; j < transportCompanies[i].HinhThucVanChuyen.Length; j++)
                    {
                        if (transportCompanies[i].HinhThucVanChuyen[j].MaHinhThucVanChuyen == transportType.ToString())
                        {
                            LbGia.Text = transportCompanies[i].HinhThucVanChuyen[j].Gia.ToString();
                            lbDonViTien.Text = transportCompanies[i].HinhThucVanChuyen[j].DonViTien;
                        }
                    }
                }
            }
        }
    }
}
